//
//  IPHomeViewController.swift
//  IPlan
//
//  Created by robot on 3/19/16.
//  Copyright © 2016 codeFighter.com. All rights reserved.
//

import UIKit
import RealmSwift

 class IPHomeViewController:UIViewController, UICollectionViewDelegate,UICollectionViewDataSource {

    var notes:[IPPlaneModel] = []
    fileprivate var colletionView: UICollectionView?
    // MARK: life cycle
    override func viewDidLoad() {
        
        super.viewDidLoad()
        navigationItem.title = "我的计划"
        view.backgroundColor = UIColor.init(netHex: 0xF1F1F1)
        setUpMianView()
        
    }

    override func viewWillAppear(_ animated: Bool) {
        
        super.viewWillAppear(animated)
        fetchDatas()
        colletionView?.reloadData()
    }
    
    func setUpMianView() {
    
       setUpRightBarItem()
       setUpCollectionView()
       
    }
    
    func fetchDatas(){
        
        let currentDate = Date.init().getCurrentDateString()
        let realm = try! Realm()

        var results = realm.objects(IPPlaneModel.self)
        if results.count > 1 {
            
          results = results.filter(NSPredicate(format: "date = %@",currentDate))
        }
        
        notes = Array(results)
        print(realm.configuration.fileURL!)

    }
       
    

    func setUpRightBarItem() {
    
        navigationItem.rightBarButtonItem = UIBarButtonItem.init(image: UIImage(named: "top_tianjia"), style:UIBarButtonItemStyle.plain, target: self, action: #selector(IPHomeViewController.PushToNewPlane))
        navigationItem.rightBarButtonItem?.tintColor = UIColor.red
        
    }
    
    func setUpCollectionView() {
        
        let collectionViewFlowLayout = UICollectionViewFlowLayout.init()
        collectionViewFlowLayout.scrollDirection = .vertical
        
        let collectionGap:CGFloat = 12.0
        collectionViewFlowLayout.minimumInteritemSpacing = collectionGap
        collectionViewFlowLayout.minimumLineSpacing = collectionGap
        collectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(12, 12, 12, 12)
        let colletionViewW = (SCREEN_W - collectionGap*3) / 2
        collectionViewFlowLayout.itemSize = CGSize(width: colletionViewW , height: 100)
        colletionView = UICollectionView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_W, height: SCREEN_H - 64 - 49), collectionViewLayout: collectionViewFlowLayout)
        colletionView?.contentOffset = CGPoint.zero;
        let homeItemNib = UINib(nibName: "IPHomeItemCell", bundle: nil)
        colletionView?.register(homeItemNib, forCellWithReuseIdentifier: "IPHomItemIdentifier")
        colletionView?.backgroundColor = UIColor.init(netHex: 0xF1F1F1)
        colletionView?.delegate = self
        colletionView?.dataSource = self
        view.addSubview(colletionView!)
        
    }
    
     func PushToNewPlane(){
    
     let newPlanController = IPNewPlanViewController()
     navigationController! .pushViewController(newPlanController, animated: true)
    
    }

     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return notes.count;
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let collectionViewCell : IPHomeItemCell = collectionView .dequeueReusableCell(withReuseIdentifier: "IPHomItemIdentifier", for: indexPath) as! IPHomeItemCell
        let planeModel = notes[indexPath.row]
        collectionViewCell.setContent(tile: planeModel.content)
        return collectionViewCell
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        print("click on \((indexPath as NSIndexPath).row) row \((indexPath as NSIndexPath).section) section")
        let planDetialController = IPPlanDetialController()
        let planeModel = notes[indexPath.row]
        planDetialController.planContent = planeModel.content
        navigationController?.pushViewController(planDetialController, animated: true)
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}




